Ext.ns("PG"); // 分页 @author hejinguo
PG.PagingBar = Ext.extend(Ext.PagingToolbar, {
	constructor : function(b) {
		var a = {
			pageSize : b.store.baseParams.limit ? b.store.baseParams.limit : 25,
			isShowTwoHunder : false,
			isShowPicHunder : false,
			displayInfo : true,
			// displayMsg : "当前显示从{0}至{1}， 共{2}条记录",
			// emptyMsg : "当前没有记录",
			plugins : [new Ext.ux.plugins.PageComboResizer()]
		};
		Ext.apply(a, b);
		PG.PagingBar.superclass.constructor.call(this, a);
	},
	isRefush : false,
	initComponent : function() {
		PG.PagingBar.superclass.initComponent.call(this);
		if (this.isRefush == true) {
			this.refresh.hide();
		}
	}
});
/**
 * 图片处理特殊分页
 * 
 * @class PG.PagingPicBar
 * @extends Ext.PagingToolbar
 */
PG.PagingPicBar = Ext.extend(Ext.PagingToolbar, {
			constructor : function(b) {
				var a = {
					pageSize : b.store.baseParams.limit
							? b.store.baseParams.limit
							: 200,
					isShowTwoHunder : false,
					isShowPicHunder : false,
					displayInfo : true,
					// displayMsg : "当前显示从{0}至{1}， 共{2}条记录",
					// emptyMsg : "当前没有记录",
					plugins : [new Ext.ux.plugins.PageComboResizer()]
				};
				Ext.apply(a, b);
				PG.PagingPicBar.superclass.constructor.call(this, a);
			},
			isRefush : false,
			initComponent : function() {
				PG.PagingPicBar.superclass.initComponent.call(this);
				if (this.isRefush == true) {
					this.refresh.hide();
				}
			}
		});


// TabPanel自适应方法，适合中间有两个面板 @author hejinguo
var fieldSetListeners = function(idarr, heightarr) {
	var allFieldSet = Ext.getCmp(idarr[0]);
	allFieldSet.on('collapse', function() {
				var allForm = Ext.getCmp(idarr[1]);
				var allGrid = Ext.getCmp(idarr[2]);
				var height = Ext.getCmp("centerTabPanel").getActiveTab()
						.getInnerHeight();
				allForm.setHeight(height * 0.05);
				allGrid.setHeight(height * 0.95);
			});
	allFieldSet.on('expand', function() {
				var allForm = Ext.getCmp(idarr[1]);
				var allGrid = Ext.getCmp(idarr[2]);
				var height = Ext.getCmp("centerTabPanel").getActiveTab()
						.getInnerHeight();
				allForm.setHeight(height * heightarr[0]);
				allGrid.setHeight(height * heightarr[1]);
			});
};

/* 公用树下拉选择控件 @author hejinguo* */
var TreeSelector = function(comboxId, url, label, hiddenId, allowBlank, width) {
	var combox = {
		id : comboxId,
		store : new Ext.data.SimpleStore({
					fields : [],
					data : [[]]
				}),
		editable : false,
		mode : "local",
		fieldLabel : label,
		allowBlank : allowBlank == false ? false : true,
		triggerAction : "all",
		selectOnFocus : false,
		emptyText : '请选择',
		anchor : '100%',
		width : width == null ? '' : width,
		tpl : "<tpl for='.'><div style='height:300px;'><div id='" + comboxId
				+ "tree'></div></div></tpl>",
		selectedClass : ""
	};
	var comboxTree = new Ext.form.ComboBox(combox);
	var treePanel = new Ext.tree.TreePanel({
				id : comboxId + "Tree",
				autoScroll : true,
				// collapsible : true,
				split : true,
				loader : new Ext.tree.TreeLoader({
							url : url
						}),
				root : new Ext.tree.AsyncTreeNode({
							expanded : true
						}),
				rootVisible : false
			});
	treePanel.on("collapsenode", function(i) {
				comboxTree.expand();
			});
	treePanel.on("expandnode", function(i) {
				comboxTree.expand();
			});
	treePanel.on("click", function(node) {
				treePanel.clickNodes = true;
				var i = Ext.getCmp(hiddenId);
				if (node.id != 0) {
					if (node.id != null && node.id != "") {
						comboxTree.setValue(node.text);
						comboxTree.id = node.id;
						comboxTree.collapse();
						if (i != null) {
							i.setValue(node.id);
						}
					}
				}
			});
	comboxTree.on("expand", function() {
				treePanel.render(comboxId + "tree");
				treePanel.expandAll();
			});

	return comboxTree;
};
/**
 * 岗位树选择
 * 
 * @author Hi
 * 
 * @param {}
 *            url
 * @param {}
 *            val
 * @return {}
 */
var postSelectTree = function(url, val) {
	var tcp = new Ext.ux.TreeCheckPanel({
				id : "_postSelectTree",
				onlyLeafCheckable : false,
				autoScroll : true,
				rootVisible : false,
				animate : true,
				lines : true,
				loader : new Ext.ux.TreeCheckLoader({
							dataUrl : url,
							preloadChildren : true
						}),
				root : new Ext.tree.AsyncTreeNode({
							expanded : true
						})
			});
	tcp.expandAll();
	tcp.on('expandnode', function(node) {
				var tree = node.getOwnerTree();
				if (val != null && val.toString().length > 0) {
					var strs = val.split(',');
					for (var i = 0; i < strs.length; i++) {
						if (node.id == strs[i]) {
							node.getUI().checkbox.checked = true;
							node.attributes.checked = true;
							tree.fireEvent('check', node, true);
						}
					}
				}
			});

	var win = new Ext.Window({
		title : "岗位选择",
		width : 300,
		height : 400,
		modal : true,
		layout : "fit",
		plain : true,
		items : [tcp],
		buttonAlign : "center",
		resizable : false,
		closeAction : 'close',
		buttons : [{
					text : "确定",
					handler : function() {
						var nodes = tcp.getChecked();
						var postId = '';
						var postText = '';
						if (nodes.length > 0) {
							for (var i = 0; i < nodes.length; i++) {
								postId += nodes[i].id;
								postId += ',';
								postText += nodes[i].text;
								postText += ',';
							}
							postId = postId.substring(0, postId.length - 1);
							postText = postText.substring(0, postText.length
											- 1);
						}
						Ext.getCmp('addPriAdmin_postId').setValue('');
						Ext.getCmp('addPriAdmin_treePostSelector').setValue('');
						Ext.getCmp('addPriAdmin_postId').setValue(postId);
						Ext.getCmp('addPriAdmin_treePostSelector')
								.setValue(postText);
						win.close();
					}
				}, {
					xtype : "tbseparator"
				}, {
					text : "取消",
					handler : function() {
						win.close();
					}
				}]
	});
	return win;
};


// 重写MessageBox提示框大小 @author hejinguo
Ext.override(Ext.BoxComponent, {
			setSize : function(b, d) {
				if (typeof b == "object") {
					d = b.height, b = b.width;
				}
				if (Ext.isDefined(b) && Ext.isDefined(this.minWidth)
						&& (b < this.minWidth)) {
					b = this.minWidth;
				}
				if (Ext.isDefined(d) && Ext.isDefined(this.minHeight)
						&& (d < this.minHeight)) {
					d = this.minHeight;
				}
				if (Ext.isDefined(b) && Ext.isDefined(this.maxWidth)
						&& (b > this.maxWidth)) {
					b = this.maxWidth;
				}
				if (Ext.isDefined(d) && Ext.isDefined(this.maxHeight)
						&& (d > this.maxHeight)) {
					d = this.maxHeight;
				}
				if (!this.boxReady) {
					this.width = b, this.height = d;
					return this;
				}
				if (this.cacheSizes !== false && this.lastSize
						&& this.lastSize.width == b
						&& this.lastSize.height == d) {
					return this;
				}
				this.lastSize = {
					width : b,
					height : d
				};
				var c = this.adjustSize(b, d), f = c.width, a = c.height, e;
				if (f !== undefined || a !== undefined) {
					e = this.getResizeEl();
					if (!this.deferHeight && f !== undefined && a !== undefined) {
						e.setSize(f, a);
					} else {
						if (!this.deferHeight && a !== undefined) {
							e.setHeight(a);
						} else {
							if (f !== undefined) {
								e.setWidth(f);
							}
						}
					}
					this.onResize(f, a, b, d);
				}
				return this;
			},
			onResize : function(d, b, a, c) {
				this.fireEvent("resize", this, d, b, a, c);
			}
		});
/** **************************Ext.form.RadioGroup****************************************** */
// 重写RadioGroup 获取和赋值方法 hejinguo
Ext.override(Ext.form.RadioGroup, {
			getValue : function() {
				var v;
				this.items.each(function(item) {
							if (item.getValue()) {
								v = item.getRawValue();
								return false;
							}
						});
				return v;
			},
			setValue : function(v) {
				if (this.rendered) {
					this.items.each(function(item) {
								item.setValue(item.inputValue == v);
							});
				} else {
					for (k in this.items) {
						this.items[k].checked = this.items[k].inputValue == v;
					}
				}
			}
		});
